from .client import ApiClient
from urllib import parse
import time
from ..utils.hashs import HashUtils
"""
EUIClient
EUI链接管理
"""

class EUIClient(ApiClient):

    # 获取EUI域名
    def get_eui_domain(self):
        domains = {
            'https://sit-api.fadada.com/api/v5/' : 'https://'+self.app_id + '.sit-e.fadada.com',
            'https://uat-api.fadada.com/api/v5/' : 'https://'+self.app_id + '.uat-e.fadada.com',
            'https://api.fadada.com/api/v5/' : 'https://'+self.app_id + '.e.fadada.com'
        }
        return domains[self.request_url]

    # 构造企业用户授权链接
    # https://{appId}.e.fadada.com/authorize/list?authScopes={authScopes}&corpName={corpName}&openCorpId={openCorpId}&redirectUrl={URLEncode(Url)}&signature={signature}&timestamp={timestamp}
    def compose_corp_auth_url(self, data):
        time_stamp = str(int(round(time.time() * 1000)))
        # 请求参数拼接
        redirectUrl=parse.quote(data['redirectUrl'],'utf-8')
        data['redirectUrl']=redirectUrl
        sort_params_str = HashUtils.sort_params_skip_empty(data)
        sign_str = HashUtils.sign(sort_params_str, time_stamp, self.app_secret)
        result_url = EUIClient.get_eui_domain(self)+'/authorize/list?authScopes='+data['authScopes']+'&corpName='+data['corpName']+'&openCorpId='+data['openCorpId']+'&redirectUrl='+redirectUrl+'&signature='+ sign_str +'&timestamp='+ time_stamp
        return result_url